package com.rjkj.medicalwaste.viewmodels

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.rjkj.medicalwaste.base.BaseViewModel
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.network.response.BaseResponse
import com.rjkj.medicalwaste.network.response.deal
import com.rjkj.medicalwaste.network.response.next
import com.rjkj.medicalwaste.repository.GarbageRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class CollectMainViewModel(
         val garbageRepository: GarbageRepository,
) : BaseViewModel() {
    var canExit = true
     val changeList = mutableListOf<GarbageBean>()
    private var controlBoxList = listOf<GarbageBean>()
    val list = MutableLiveData(listOf<GarbageBean>())
    fun addGarbageToList(garbageBean: GarbageBean) {
        changeList.remove(garbageBean)
        changeList.add(garbageBean)
        val value = mutableListOf<GarbageBean>()
        value.addAll(changeList)
        value.addAll(controlBoxList)
        list.value = value
    }
    fun cleanList(){
        changeList.clear()
        controlBoxList = listOf()
        list.value = listOf()
    }
    fun addGarbageListToList(garbageBean: List<GarbageBean>) {
        controlBoxList = garbageBean
        val value = mutableListOf<GarbageBean>()
        value.addAll(changeList)
        value.addAll(controlBoxList)
        list.value = (value)
    }

    fun printGarbageList() {
        val value = mutableListOf<GarbageBean>()
        value.addAll(changeList)
        value.addAll(controlBoxList)
        for (garbageBean in value) {
            garbageBean.print = true
        }
        list.value = value
    }

//    fun getPagingData(hospitalId: String): Flow<PagingData<GarbageBean>> {
//        return Pager(config = PagingConfig(
//                pageSize = 6,
//                enablePlaceholders = false,
//                initialLoadSize = 6
//        ), pagingSourceFactory = {
//            garbageRepository.getAllCollectGarbagePagingSource(hospitalId).also {
//                it.invalid
//            }
//        }).flow
//    }

    fun deleteGarbage(garbageBean: GarbageBean) {

        val value = mutableListOf<GarbageBean>()
        value.addAll(changeList)
        value.addAll(controlBoxList)
        for (i in value.indices) {
            if (changeList[i].garbageQRCode == garbageBean.garbageQRCode) {
                changeList.removeAt(i)
                break
            }
        }
        list.value = changeList

    }

    suspend fun findPackByDepartment(url:String,departmentList: List<String>): List<PackBean> {
        val deal = request(false) {
            findPackByDepartment(url = url, departmentList = departmentList)
        }.deal()
        return if (deal.isNullOrEmpty()) {
            listOf()
        } else {
            deal
        }

    }

    suspend fun addAllGarbageToDatabase(list: List<GarbageBean>) {
        try {
            garbageRepository.insertGarbageList(list)
        }catch (e:Throwable){
            e.printStackTrace()
        }

    }
//    fun getLastPagingData(): Flow<PagingData<TestBean>> {
//        int--
//        return Repository.getPagingData(pageNum)
//    }
//    fun getNextPagingData(): Flow<PagingData<TestBean>> {
//        int++
//        return Repository.getPagingData(pageNum)
//    }
}